/*
 * Created by JFormDesigner on Mon Nov 28 02:23:05 MSK 2011
 */

package view;

import calendar.Day;
import task.Task;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author Pavel
 */
public class AddNoteForm extends JFrame {

    private static AddNoteForm instance = new AddNoteForm();

    private Day day;
    private Organizer organizer;

    public static AddNoteForm getInstance() {
        return instance;
    }

    private AddNoteForm() {
        initComponents();
    }

    public void setModel(Organizer organizer, Day day) {
        this.organizer = organizer;
        this.day = day;
    }

    private void AddNoteButtonMouseClicked(MouseEvent e) {
        System.out.println(noteArea.getText());
        day.addTask(new Task(noteArea.getText()));
        noteArea.setText("");
        this.setVisible(false);
        organizer.updateNotes();
    }

    private void thisWindowStateChanged(WindowEvent e) {
        noteArea.setText("");
    }

    private void noteAreaKeyPressed(KeyEvent e) {
        int modifiers = e.getModifiers();
        if(e.getKeyCode() == KeyEvent.VK_ENTER && modifiers == KeyEvent.CTRL_MASK) {
            System.out.println(noteArea.getText());
            day.addTask(new Task(noteArea.getText()));
            noteArea.setText("");
            this.setVisible(false);
            organizer.updateNotes();
        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        scrollPane1 = new JScrollPane();
        noteArea = new JTextArea();
        addNoteButton = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== scrollPane1 ========
        {

            //---- noteArea ----
            noteArea.setFont(new Font("Consolas", Font.ITALIC, 18));
            noteArea.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    noteAreaKeyPressed(e);
                }
            });
            scrollPane1.setViewportView(noteArea);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(15, 5, 370, 210);

        //---- addNoteButton ----
        addNoteButton.setText("Add");
        addNoteButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                AddNoteButtonMouseClicked(e);
            }
        });
        contentPane.add(addNoteButton);
        addNoteButton.setBounds(310, 225, 73, addNoteButton.getPreferredSize().height);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JScrollPane scrollPane1;
    private JTextArea noteArea;
    private JButton addNoteButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
